'use client'; import { useState, type FormEvent } from 'react'; import { useRouter } from 'next/navigation'; // Customer-facing cancel button shown on the order detail page only // when the order is in a cancellable state (currently `pending_design` // — checked server-side too in /api/orders/[id]/cancel). // // Two-step UX: a "Cancel this commission" link reveals a confirmation // panel with the refund amount + reassurance + Cancel/Keep buttons. // Native confirm() would technically work but reads as untrusted on a // page like this where the customer just paid real money. export function CancelButton({ orderId, priceCents, paid = true, refundPending = false, }: { orderId: string; priceCents: number; paid?: boolean; refundPending?: boolean; }) { const router = useRouter(); const [mode, setMode] = useState<'cancel' | 'replace' | null>(refundPending ? 'cancel' : null); const [submitting, setSubmitting] = useState(false); const [error, setError] = useState(null); const amount = `$${(priceCents / 100).toFixed(2)}`; const onCancel = async (e: FormEvent) => { e.preventDefault(); setSubmitting(true); setError(null); try { const res = await fetch(`/api/orders/${orderId}/cancel`, { method: 'POST' }); if (!res.ok) { const data = await res.json().catch(() => null) as { error?: string } | null; throw new Error(data?.error ?? `Cancel failed (${res.status})`); } // Soft-refresh so the status pill + the cancelled body copy // update. The parent only renders this button while status is // `pending_design`, so after refresh the CancelButton unmounts // entirely. But we still reset submitting + close the panel so // the UI doesn't look hung during the brief refresh window. setSubmitting(false); setMode(null); if (mode === 'replace') router.push(`/builder?edit=${encodeURIComponent(orderId)}&replace=1`); else router.refresh(); } catch (err) { setError(err instanceof Error ? err.message : 'Cancel failed.'); setSubmitting(false); if (!refundPending) router.refresh(); } }; if (!mode) { return (
{' · '}
); } return (
{refundPending ? 'Confirm your refund' : mode === 'replace' ? 'Cancel this order and start a replacement?' : 'Cancel this commission?'}

{paid && <>We’ll request a full refund of {amount} to your original payment method. } Your uploaded photos stay in Saved photos. {mode === 'replace' && ' We’ll open a new order with your photos and selections. You can change them before checking out again.'}

{!refundPending && }
{error &&

{error}

}
); }